using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using Microsoft.Win32;
using System.Windows.Forms;

namespace Weazel.Toad.Application.Forms
{
  public partial class MicroblazeOpbSynthesisForm 
    : Weazel.Toad.Application.Forms.DialogBaseForm
  {
    private List<string> datapaths;
    public List<string> Datapaths
    {
      get { return datapaths; }
      set { datapaths = value; }
    }

    private string selectedDatapath;
    public string SelectedDatapath
    {
      get { return selectedDatapath; }
    }

    private List<string> ipBlocks;
    public List<string> IpBlocks
    {
      get { return ipBlocks; }
      set { ipBlocks = value; }
    }

    private string selectedIpBlock;
    public string SelectedIpBlock
    {
      get { return selectedIpBlock; }
    }

    private string ipifName;
    public string IpifName
    {
      get { return ipifName; }
      set { ipifName = value; }
    }

    public int MajorVersion
    {
      get { return (int) numeric_major.Value; }
    }

    public int MinorVersion
    {
      get { return (int) numeric_minor.Value; }
    }

    public string Revision
    {
      get { return cb_revision.SelectedItem as string; }
    }

    public string Directory
    {
      get { return tb_directory.Text; }
      set { tb_directory.Text = value; }
    }

    protected override void OnClosing(CancelEventArgs e)
    {
      if (DialogResult == DialogResult.OK)
      {
        if (cb_datapaths.SelectedIndex == 0)
        {
          // the dummy "<selcet datapath>" is selected
          e.Cancel = true;
        }
        else
        {
          selectedDatapath =
            datapaths[cb_datapaths.SelectedIndex - 1];
        }

        if (cb_ipBlocks.SelectedIndex == 0)
        {
          // dummy "<select OPB Ipblock> selected
          e.Cancel = true;
        }
        else
        {
          selectedIpBlock =
            ipBlocks[cb_ipBlocks.SelectedIndex - 1];
        }

        if (tb_directory.Text == "" || !System.IO.Directory.Exists(tb_directory.Text))
        {
          MessageBox.Show("Invalid output directory");
          e.Cancel = true;
        }

        // store the destination dir to the registry
        Application.Instance.SettingsRegistry.SetValue("opb_ipif_export_destination", tb_directory.Text);
      }
    }

    protected override void OnLoad(EventArgs e)
    {
      System.Diagnostics.Trace.Assert(datapaths != null);
      System.Diagnostics.Trace.Assert(ipBlocks != null);
      System.Diagnostics.Trace.Assert(ipifName != null);

      // populate the "datapaths" combobox list
      cb_datapaths.Items.Clear();
      cb_datapaths.Items.Add("<select datapath>");

      foreach (string dp in datapaths)
        cb_datapaths.Items.Add(dp);

      // select the dummy "<select datapath>"
      cb_datapaths.SelectedIndex = 0;

      // populate the "ip-blocks" combobox list
      cb_ipBlocks.Items.Clear();
      cb_ipBlocks.Items.Add("<select OPB ip-block>");

      foreach (string ipb in ipBlocks)
        cb_ipBlocks.Items.Add(ipb);

      cb_ipBlocks.SelectedIndex = 0;

      // populate "version/revision"
      this.numeric_major.Value = 1;
      this.numeric_minor.Value = 0;
      this.cb_revision.SelectedIndex = 0; // rev "a"

      // name ..
      this.tb_name.Text = IpifName;


      // override target directory if previously set
      string dir = Application.Instance.SettingsRegistry.GetValue("opb_ipif_export_destination") as string;
      if (dir != null && dir != "" && System.IO.Directory.Exists(dir))
        this.tb_directory.Text = dir;
    }

    public MicroblazeOpbSynthesisForm()
    {
      InitializeComponent();
    }

    private void storeDirectory(string dir)
    {
      RegistryKey regKey, regSubKey;
      regKey = Registry.CurrentUser;
      regSubKey = regKey.CreateSubKey("Toad");
      regSubKey.SetValue("opb_ipif_export_desintation", dir);
      regSubKey.Close();
      regKey.Close();
    }


    private string loadDirectory()
    {
      RegistryKey regKey, regSubKey;
      regKey = Registry.CurrentUser;
      regSubKey = regKey.OpenSubKey("Toad");
      string result = regSubKey.GetValue("opb_ipif_export_desintation") as string;
      regSubKey.Close();
      regKey.Close();
      return result;
    }

    private void button1_Click(object sender, EventArgs e)
    {
      FolderBrowserDialog dialog = new FolderBrowserDialog();
      
      if (System.IO.Directory.Exists(tb_directory.Text))
        dialog.SelectedPath = tb_directory.Text;
      
      if (dialog.ShowDialog() == DialogResult.OK)
      {
        this.tb_directory.Text = dialog.SelectedPath;
      }
    }
  }
}

